package com.example.weizix.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.example.weizix.R;
import com.miguelcatalan.materialsearchview.MaterialSearchView;

public class SearchActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private MaterialSearchView mSearchView;
    private FrameLayout mToolbarContainer;
    private RecyclerView mRlv;
    private MenuItem mSearchViewItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu, menu);
        mSearchViewItem = menu.findItem(R.id.action_search);
        mSearchView.setMenuItem(mSearchViewItem);
        return super.onCreateOptionsMenu(menu);
    }

    private void initView() {

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mSearchView = (MaterialSearchView) findViewById(R.id.search_view);
        mToolbarContainer = (FrameLayout) findViewById(R.id.toolbar_container);
        mRlv = (RecyclerView) findViewById(R.id.rlv);

        setSupportActionBar(mToolbar);

        mSearchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //搜索内容提交监听
                //ToastUtil.showToastShort("Submit:"+query);
                //在这里进行网络

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //搜索发生改变的监听
                //ToastUtil.showToastShort("changed:"+newText);
                return false;
            }
        });

        mSearchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
            @Override
            public void onSearchViewShown() {
                //搜索框显示
                //ToastUtil.showToastShort("show");
            }

            @Override
            public void onSearchViewClosed() {
                //搜索框隐藏
                //ToastUtil.showToastShort("close");
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (mSearchView.isSearchOpen()){
            //搜索框展开状态
            mSearchView.closeSearch();
        }else {
            super.onBackPressed();
        }

    }
}
